﻿''' <summary>
''' This class implements a writer that generates an HTML file.
''' </summary>
''' <remarks>
''' The HTML writer creates a simple HTML output file. It is text
''' based and does not include any visual goodies.
''' </remarks>
Public Class HTMLWriter
    Implements IWriter

    ''' <summary>
    ''' Stream writer for HTML output.
    ''' </summary>
    ''' <remarks></remarks>
    Protected SW As System.IO.StreamWriter

    ''' <summary>
    ''' Keep track of how many DIV elements we've pushed.
    ''' </summary>
    ''' <remarks></remarks>
    Protected divPush As Integer

    ''' <summary>
    ''' Output file.
    ''' </summary>
    ''' <remarks></remarks>
    Protected fName As String = Nothing

    Public Sub Cleanup() Implements IWriter.Cleanup
        'Close the output file.
        SW.Close()
        SW = Nothing
    End Sub

    Public Sub Startup(ByVal cfgFile As String) Implements IWriter.Startup

        'Read the configuration from the XML file.
        Dim ds As New DataSet
        ds.ReadXml(cfgFile)

        'Get the file to output.
        fName = Convert.ToString(ds.Tables(0).Rows(0).Item("ReportOutput"))

        'Perform replacements.
        fName = fName.Replace("$YYYY$", Now.Year.ToString())
        fName = fName.Replace("$MM$", Now.Month.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$DD$", Now.Day.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$HH$", Now.Hour.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$mm$", Now.Minute.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$SS$", Now.Second.ToString().PadLeft(2, "0"c))

        'Open the file.
        SW = New System.IO.StreamWriter(fName, False, System.Text.Encoding.Default)

        divPush = 0

        ds.Dispose()
        ds = Nothing

    End Sub

    Public Sub WriteBlank() Implements IWriter.WriteBlank
        WriteEntry("")
    End Sub

    Public Sub WriteBlankEntry() Implements IWriter.WriteBlankEntry
        WriteEntry("")
    End Sub

    Public Sub WriteCategory(ByVal str As String) Implements IWriter.WriteCategory
        If divPush > 0 Then
            If divPush = 3 Then
                SW.WriteLine("</table>" + vbCrLf)
            End If
            For i As Integer = 1 To divPush
                WriteDivClose()
            Next
        End If
        SW.WriteLine("<DIV id=""subA"">" + vbCrLf + _
                     "<table border=""0"" width=""100%"" cellpadding=""2"" cellspacing=""10"">" + vbCrLf + _
                     "<td width=""100%"" bgcolor=""#FFFFFF""><p><font color=""blue"" size=""2""><b>&nbsp;&nbsp;<U id=""clickable"">" + str + "</U></font></b></p></td></table>" + vbCrLf)
        divPush += 1
    End Sub

    Public Sub WriteCategoryError(ByVal str As String) Implements IWriter.WriteCategoryError
        WriteSingleLineEntry(str, "#FF0000")
        If divPush = 3 Then
            SW.WriteLine("</table>" + vbCrLf)
            WriteDivClose()
            WriteDivClose()
        End If
    End Sub

    Public Sub WriteEntry(ByVal str As String) Implements IWriter.WriteEntry
        SW.WriteLine("<tr><td width=""4%"" align=""left""><font size=""1""></font></td>" + vbCrLf + _
                     "<td width=""96%"" align=""left"" colspan=""2""><font size=""1"">&nbsp;" + str + "</font></td></tr>" + vbCrLf)
    End Sub

    Public Sub WriteEntry(ByVal id As String, ByVal value As String) Implements IWriter.WriteEntry
        SW.WriteLine("<tr><td width=""4%"" align=""left""><font size=""1""></font></td>" + vbCrLf + _
                     "<td width=""26%"" align=""left""><font size=""1"">&nbsp;" + id + "</font></td>" + vbCrLf + _
                     "<td width=""70%""><font size=""1"">" + value + "</font></td></tr>")
    End Sub

    Public Sub WriteEntryError(ByVal str As String) Implements IWriter.WriteEntryError
        WriteSingleLineEntry(str, "#FF0000")
    End Sub

    Public Sub WriteFooter() Implements IWriter.WriteFooter
        If divPush > 0 Then
            For i As Integer = 1 To divPush
                WriteDivClose()
            Next
        End If
        SW.WriteLine("</body></html>")
    End Sub

    Public Sub WritePageBreak() Implements IWriter.WritePageBreak
        'No implementation
    End Sub

    Public Sub WriteSection(ByVal str As String) Implements IWriter.WriteSection
        If divPush = 3 Then
            SW.WriteLine("</table>" + vbCrLf)
            WriteDivClose()
            WriteDivClose()
        End If
        SW.WriteLine("<DIV id=""subB"">" + vbCrLf + _
                     "<table border=""0"" width=""100%"" cellpadding=""0"" cellspacing=""5"">" + vbCrLf + _
                     "<td width=""2%"" align=""left""><font size=""1""></font></td>" + vbCrLf + _
                     "<td width=""98%"" align=""left""><font size=""1"" color=""blue""><li></li><U id=""clickable"">" + str + "</u></font></td>" + vbCrLf + _
                     "</table>" + vbCrLf)

        SW.WriteLine("<DIV id =""subC"">" + vbCrLf + _
                     "<table border=""0"" width=""100%"" cellpadding=""0"" cellspacing=""2"">" + vbCrLf)
        divPush += 2
    End Sub

    Public Sub WriteSectionError(ByVal str As String) Implements IWriter.WriteSectionError
        WriteSingleLineEntry(str, "#FF0000")
        If divPush = 2 Then
            SW.WriteLine("</table>" + vbCrLf)
            WriteDivClose()
        End If
    End Sub

    Public Sub WriteSystem(ByVal str As String) Implements IWriter.WriteSystem
        SW.WriteLine("<p align=""center""><font size=""3"" color=""#000080""><b>" + str + "</b></font></p>" + vbCrLf)
    End Sub

    Public Sub WriteTitle(ByVal str As String) Implements IWriter.WriteTitle
        SW.WriteLine("<HTML><HEAD><STYLE TYPE=""text/css"">" + vbCrLf + _
                     "U{cursor:hand}" + vbCrLf + _
                     "A {color:darkblue;font-family:""Tahoma"";font-size:8.0pt;}" + vbCrLf + _
                     "P {color:darkblue;font-family:""Tahoma"";font-size:8.0pt;}" + vbCrLf + _
                     "DIV {color:lightblue;text-decoration:none;font-family:""Tahoma"";font-size:8.0pt;}" + vbCrLf + _
                     "DIV DIV{color:black;cursor:text;display:none;text-decoration:none}" + vbCrLf + _
                     "</STYLE>" + vbCrLf + _
                     "<script>" + vbCrLf + _
                     "function handleClick(){el=event.srcElement;if (el.id!=""clickable"") return; if (!changeSetting(el,""subB"",true)) changeSetting(el,""subA"",true);event.cancelBubble=true}" + vbCrLf + _
                     "function openSelected(){ s = document.selection; if ((s == null) || (s.type != ""Text"")) return; p=s.createRange().parentElement(); if ((p==null)||(p.parentElement==null))return; p=p.parentElement.parentElement; changeSetting(p,""subA"",false); changeSetting(p,""subC"",false);}" + vbCrLf + _
                     "function changeSetting(el,id,toggle){ while (true){ if (el==null) return false; if (el.id==id) break; el=el.parentElement;}" + vbCrLf + _
                     "els=el.children; for(i = 0;i<els.length;i++){el=els[i]; st=el.style; if (el.id==""clickable"")continue; if((""""==st.display)||(!toggle)){st.display=""block""; if (toggle&&(id==""subA"")) el.parentElement.scrollIntoView(true);}else st.display=""""} return true;}" + vbCrLf + _
                    "document.onclick = handleClick" + vbCrLf + _
                    "</script>" + vbCrLf + _
                    "</head>" + vbCrLf + _
                    "<body onselect=""openSelected()"">" + vbCrLf + _
                    "<hr>" + vbCrLf + _
                    "<p align=""center""><font size=""5"" color=""#000080""><b>" + str + "</b></font></p>" + vbCrLf)
    End Sub

    Public Sub WriteVersion(ByVal str As String) Implements IWriter.WriteVersion
        SW.WriteLine("<p align=""center""><font size=""0"" color=""#000080""><b>v." + str + "</b></font></p><hr>" + vbCrLf)
    End Sub

    Private Sub WriteDivClose()
        SW.WriteLine("</DIV>" + vbCrLf)
        divPush -= 1
    End Sub

    Private Sub WriteSingleLineEntry(ByVal str As String, ByVal color As String)
        SW.WriteLine("<tr><td width=""4%"" align=""left""><font size=""1""></font></td>" + vbCrLf + _
                     "<td width=""96%"" align=""left"" colspan=""2""><font size=""1"" color=""" + color + """>&nbsp;" + str + "</font></td></tr>" + vbCrLf)
    End Sub

    Public Function GetOutputFile() As String Implements IWriter.GetOutputFile
        Return fName
    End Function

End Class
